package com.net.xpay.core.manager.impl.auto;

import com.google.common.base.Preconditions;
import com.net.common.annotation.WriteTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractVersionedManager;
import com.net.noah.core.service.SerialNumberService;
import com.net.xpay.core.dao.auto.AutoPayReceiptAccountMapper;
import com.net.xpay.core.domain.auto.AutoPayReceiptAccount;
import com.net.xpay.core.manager.auto.AutoPayReceiptAccountManager;
import com.net.xpay.core.model.AutoPayReceiptAccountSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;

import java.util.List;


@Component
public class AutoPayReceiptAccountManagerImpl extends AbstractVersionedManager<AutoPayReceiptAccount> implements AutoPayReceiptAccountManager{

    @Autowired
    private AutoPayReceiptAccountMapper mapper;


    @Override
    protected BasicMapper<Long, AutoPayReceiptAccount> getMapper() {
        return mapper;
    }

    @Autowired
    private SerialNumberService serialNumberService;

    @Override
    @WriteTransactional(propagation = Propagation.SUPPORTS)
    public int save(AutoPayReceiptAccount obj) {
        Preconditions.checkArgument(obj.getId() == null);
        obj.setId(serialNumberService.getLongSerialNumber(getTableFullName()));
        return super.save(obj);
    }

    @Override
    public List<AutoPayReceiptAccount> search(AutoPayReceiptAccountSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("id", "desc");
        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public long searchCount(AutoPayReceiptAccountSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);

        return mapper.searchCount(searchModel);
    }

    @Override
    public AutoPayReceiptAccount findById(Long userId, Long id) {
        Preconditions.checkArgument(userId != null && id != null);
        AutoPayReceiptAccount condition = new AutoPayReceiptAccount();
        condition.setUserId(userId);
        condition.setId(id);
        return getByCondition(condition);
    }
}
